package com.jingdianjichi.subject.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.ES.自定义ES_鸡翅.entity.SubjectInfoES;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 刷题Controller
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    //测试用的
    @Resource
    private SubjectCategoryService subjectCategoryService;

    @GetMapping("/test")
    public String test(){
        SubjectCategory subjectCategory = subjectCategoryService.queryById(1L);
        return "hello 鸡翅"+subjectCategory.getCategoryName();
    }


    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkArgument(!StringUtils.isEmpty(subjectInfoDTO.getSubjectName()),"题目名称不能是空的");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能是空的");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能是空的");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能是空的");
            //答案可以是空的,比如我们的简答题就是选项

            //分类id不能是空的
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"分类Id不能是空的");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"标签Id不能是空的");

            //转换对象
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.convertListDTOToBO(subjectInfoDTO.getOptionList());

            subjectInfoBO.setOptionList(subjectAnswerBOS);

            subjectInfoDomainService.add(subjectInfoBO);

            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e);

            return Result.fail(e.getMessage());
        }
    }


    /**
     * 题目列表分页查询
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }


            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能是空的");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能是空的");


            //分类id不能是空的
            Preconditions.checkArgument(CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"分类Id不能是空的");
            Preconditions.checkArgument(CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"标签Id不能是空的");

            //转换对象
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);

            PageResult<SubjectInfoBO> boPageResult=subjectInfoDomainService.getSubjectPage(subjectInfoBO);

            PageResult<SubjectInfoDTO> dtoPageResult= SubjectInfoDTOConverter.INSTANCE.converBoPageToDTOPageList(boPageResult);

            return Result.ok(dtoPageResult);
        }catch (Exception e){
            log.error("SubjectCategoryController.getSubjectPage.error:{}",e.getMessage(),e);

            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询题目详情
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能是空的");

            //转换对象
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);

            SubjectInfoBO boResult=subjectInfoDomainService.querySubjectInfo(subjectInfoBO);

            SubjectInfoDTO dto=SubjectInfoDTOConverter.INSTANCE.convertBoToDTO(boResult);

            return Result.ok(dto);
        }catch (Exception e){
            log.error("SubjectCategoryController.querySubjectInfo.error:{}",e.getMessage(),e);

            return Result.fail("查询题目详情失败");
        }
    }


    /**
     * 全文检索,高亮返回,我们先不转亲
     */
    @PostMapping("/querySubjectPageBySearch")
    public Result<PageResult<SubjectInfoES>> querySubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()),"关键词不能是空的!");

            //转换对象
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());

            PageResult<SubjectInfoES> pageResult=subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);

            //不用转换了
            return Result.ok(pageResult);
        }catch (Exception e){
            log.error("SubjectCategoryController.querySubjectPageBySearch.error:{}",e.getMessage(),e);

            return Result.fail("全文检索失败");
        }
    }


    /**
     * 更新题目
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectInfoDTO subjectInfoDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.update.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能是空的");

            //转换对象
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);

            Boolean result=subjectInfoDomainService.update(subjectInfoBO);

            //不用转换了
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.update.error:{}",e.getMessage(),e);

            return Result.fail("修改题目失败");
        }
    }


    /**
     * 获取题目的贡献榜单
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList(){
        try {
            //mysql版本
            //List<SubjectInfoBO> boList=subjectInfoDomainService.getContributeList1();

            //redis版本
            List<SubjectInfoBO> boList=subjectInfoDomainService.getContributeList2();

            List<SubjectInfoDTO> dtoList=SubjectInfoDTOConverter.INSTANCE.convertBoToDTOList(boList);

            return Result.ok(dtoList);
        }catch (Exception e){
            log.error("SubjectCategoryController.getContributeList.error:{}",e.getMessage(),e);
            return Result.fail("获得贡献榜失败");
        }
    }
}
